<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="优惠劵类别" prop="couponType">
        <el-select
          v-model="queryParams.couponType"
          placeholder="请选择优惠劵类型 1现金劵，2折扣券，3兑换券"
          clearable
          size="small"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>

      <el-form-item label="油品名称" prop="oilName">
        <el-input
          v-model="queryParams.oilName"
          placeholder="请输入油品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放状态" prop="grantStatus">
        <el-select
          v-model="queryParams.grantStatus"
          placeholder="请选择发放状态"
          clearable
          size="small"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['coupon:info:add']"
          >创建优惠劵</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="id" align="left" prop="id" v-if="false" />
      <el-table-column label="优惠劵ID" align="left" prop="couponId" />
      <el-table-column label="优惠劵名称" align="left" prop="couponName" />
      <el-table-column label="油品名称" align="left" prop="oilName" />
      <el-table-column
        label="优惠劵类型"
        align="left"
        prop="couponType"
        v-if="false"
      />
      <el-table-column label="使用门槛" align="left" prop="useDiscountType" />
      <el-table-column
        label="1 柴油，2 汽油"
        align="left"
        prop="oilType"
        v-if="false"
      />
      <el-table-column label="面值" align="left" prop="couponAmt" />
      <el-table-column label="优惠劵数量" align="left" prop="couponNum" />
      <el-table-column
        label="有效时间类型"
        align="left"
        prop="effectiveTimeType"
        v-if="false"
      />
      <el-table-column
        label="固定时间-有效开始时间"
        align="left"
        prop="effectiveTimeStart"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.effectiveTimeStart, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="固定时间-有效结束时间"
        align="left"
        prop="effectiveTimeEnd"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.effectiveTimeEnd, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效天数" align="left" prop="effectiveDayNum" />
      <el-table-column
        label="优惠劵领取数量"
        align="left"
        prop="couponReceiveNum"
      />
      <el-table-column label="有效状态，1未生效" align="left" prop="status" />
      <!-- <el-table-column label="发放平台，1.微信 2支付宝" align="left" prop="openPlatform" /> -->
      <el-table-column label="发放状态" align="left" prop="grantStatus" />
      <el-table-column label="创建人" align="left" prop="createBy" />
      <el-table-column label="创建时间" align="left" prop="createTime" />
      <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['coupon:info:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['coupon:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠劵对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="优惠劵ID" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠劵ID" />
        </el-form-item>
        <el-form-item label="优惠劵名称" prop="couponName">
          <el-input v-model="form.couponName" placeholder="请输入优惠劵名称" />
        </el-form-item>
        <el-form-item label="优惠劵类型" prop="couponType">
          <el-select
            v-model="form.couponType"
            placeholder="请选择优惠劵类型 1现金劵，2折扣券，3兑换券"
          >
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="油品" prop="oilName">
          <!-- <el-input v-model="form.oilName" placeholder="请输入油品" /> -->
        </el-form-item>
        <el-form-item label="使用门槛类型" prop="useDiscountType">
          <el-select
            v-model="form.useDiscountType"
            placeholder="请选择优惠劵使用门槛类型。 1.无门槛，2满减劵，3折扣"
          >
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="优惠劵面值" prop="couponAmt">
          <el-input v-model="form.couponAmt" placeholder="请输入优惠劵面值" />元
        </el-form-item>
        <el-form-item label="优惠劵数量" prop="couponNum">
          <el-input v-model="form.couponNum" placeholder="请输入优惠劵数量" />张
        </el-form-item>
        <el-form-item label="有效时间类型" prop="effectiveTimeType">
          <el-select
            v-model="form.effectiveTimeType"
            placeholder="请选择有效时间类型 1固定时间，2领取后x天内有效"
          >
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="固定时间">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.effectiveTimeStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择固定时间-有效开始时间"
          >
          </el-date-picker>
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.effectiveTimeEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择固定时间-有效结束时间"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="有效天数" prop="effectiveDayNum">
          <el-input
            v-model="form.effectiveDayNum"
            placeholder="请输入有效天数"
          />
        </el-form-item>
        <el-form-item label="每人限领取数量" prop="couponReceiveNum">
          <el-input
            v-model="form.couponReceiveNum"
            placeholder="请输入优惠劵领取数量"
          />
        </el-form-item>
        <el-form-item label="发放平台，1.微信 2支付宝" prop="openPlatform">
          <el-input
            v-model="form.openPlatform"
            placeholder="请输入发放平台，1.微信 2支付宝"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  exportInfo,
} from "@/api/coupon/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠劵表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponId: null,
        couponName: null,
        couponType: null,
        useDiscountType: null,
        oilName: null,
        oilType: null,
        couponAmt: null,
        couponNum: null,
        effectiveTimeType: null,
        effectiveTimeStart: null,
        effectiveTimeEnd: null,
        effectiveDayNum: null,
        couponReceiveNum: null,
        openPlatform: null,
        grantStatus: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询优惠劵列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        couponId: null,
        couponName: null,
        couponType: null,
        useDiscountType: null,
        oilName: null,
        oilType: null,
        couponAmt: null,
        couponNum: null,
        effectiveTimeType: null,
        effectiveTimeStart: null,
        effectiveTimeEnd: null,
        effectiveDayNum: null,
        couponReceiveNum: null,
        openPlatform: null,
        grantStatus: "0",
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠劵";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠劵";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除优惠劵编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有优惠劵数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportInfo(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
